package servlet.admin;

import dao.WarningThresholdDao;
import entity.Admin;
import entity.WarningThreshold;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/admin/warningThreshold/*")
public class WarningThresholdServlet extends HttpServlet {
    private WarningThresholdDao thresholdDao = new WarningThresholdDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getPathInfo();

        // 检查管理员登录状态
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null) {
            response.sendRedirect(request.getContextPath() + "/adminLogin.jsp");
            return;
        }

        try {
            if ("/delete".equals(action)) {
                deleteThreshold(request, response);
            } else if ("/edit".equals(action)) {
                showEditForm(request, response);
            } else {
                response.sendRedirect(request.getContextPath() + "/admin/warningThresholdManage.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("/admin/warningThresholdManage.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getPathInfo();

        // 检查管理员登录状态
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if (admin == null) {
            response.sendRedirect(request.getContextPath() + "/adminLogin.jsp");
            return;
        }

        try {
            if ("/add".equals(action)) {
                addThreshold(request, response);
            } else if ("/update".equals(action)) {
                updateThreshold(request, response);
            } else {
                response.sendRedirect(request.getContextPath() + "/admin/warningThresholdManage.jsp");
            }
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error", e.getMessage());
            request.getRequestDispatcher("/admin/warningThresholdManage.jsp").forward(request, response);
        }
    }

    private void addThreshold(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        WarningThreshold threshold = new WarningThreshold();
        threshold.setFeeTypeId(Integer.parseInt(request.getParameter("feeTypeId")));
        threshold.setThreshold(new BigDecimal(request.getParameter("threshold")));
        threshold.setDescription(request.getParameter("description"));
        threshold.setStatus(Integer.parseInt(request.getParameter("status")));

        thresholdDao.addThreshold(threshold);
        response.sendRedirect(request.getContextPath() + "/admin/warningThresholdManage.jsp");
    }

    private void updateThreshold(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        WarningThreshold threshold = new WarningThreshold();
        threshold.setId(Integer.parseInt(request.getParameter("id")));
        threshold.setThreshold(new BigDecimal(request.getParameter("threshold")));
        threshold.setDescription(request.getParameter("description"));
        threshold.setStatus(Integer.parseInt(request.getParameter("status")));

        thresholdDao.updateThreshold(threshold);
        response.sendRedirect(request.getContextPath() + "/admin/warningThresholdManage.jsp");
    }

    private void deleteThreshold(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        thresholdDao.deleteThreshold(id);
        response.sendRedirect(request.getContextPath() + "/admin/warningThresholdManage.jsp");
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        WarningThreshold threshold = thresholdDao.getThresholdById(id);
        request.setAttribute("threshold", threshold);
        request.getRequestDispatcher("/admin/editWarningThreshold.jsp").forward(request, response);
    }
}